using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Thinxoft.HttpLib;
using Thinxoft.XedusLive.SiteFinder;

namespace Thinxoft.XedusLive
{
	/// <summary>
	/// Summary description for ConnectToNowForm.
	/// </summary>
	public class ConnectToNowForm : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Button btnConnect;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox txtEmail;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox txtApplications;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		//p2p.P2PWebService pws = new p2p.P2PWebService();
        SiteFinder.XedusSiteFinder finder = new Thinxoft.XedusLive.SiteFinder.XedusSiteFinder();

		public ConnectToNowForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            
		}

        public void LoadData()
        {
            foreach (HttpApplication app in Start.XedusState.XedusSettings.HttpApplications)
            {
                txtApplications.Items.Add(app.Name);
            }

            this.txtEmail.Text = Start.XedusState.XedusSettings.LastEmail;
        }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.btnConnect = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtPassword = new System.Windows.Forms.TextBox();
            this.label9 = new System.Windows.Forms.Label();
            this.txtEmail = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtApplications = new System.Windows.Forms.ComboBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnConnect
            // 
            this.btnConnect.Location = new System.Drawing.Point(203, 146);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(75, 23);
            this.btnConnect.TabIndex = 3;
            this.btnConnect.Text = "Connect";
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.txtPassword);
            this.groupBox1.Controls.Add(this.label9);
            this.groupBox1.Controls.Add(this.txtEmail);
            this.groupBox1.Location = new System.Drawing.Point(12, 22);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(266, 118);
            this.groupBox1.TabIndex = 16;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Required Information";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(11, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(100, 15);
            this.label1.TabIndex = 10;
            this.label1.Text = "User ID";
            // 
            // txtPassword
            // 
            this.txtPassword.Location = new System.Drawing.Point(133, 53);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.Size = new System.Drawing.Size(100, 20);
            this.txtPassword.TabIndex = 1;
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(12, 57);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(100, 14);
            this.label9.TabIndex = 18;
            this.label9.Text = "Password";
            // 
            // txtEmail
            // 
            this.txtEmail.Location = new System.Drawing.Point(133, 26);
            this.txtEmail.Name = "txtEmail";
            this.txtEmail.Size = new System.Drawing.Size(100, 20);
            this.txtEmail.TabIndex = 0;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(22, 108);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(100, 19);
            this.label2.TabIndex = 27;
            this.label2.Text = "Server Application";
            // 
            // txtApplications
            // 
            this.txtApplications.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.txtApplications.Location = new System.Drawing.Point(145, 105);
            this.txtApplications.Name = "txtApplications";
            this.txtApplications.Size = new System.Drawing.Size(103, 21);
            this.txtApplications.TabIndex = 2;
            // 
            // ConnectToNowForm
            // 
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtApplications);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.groupBox1);
            this.Name = "ConnectToNowForm";
            this.Size = new System.Drawing.Size(285, 177);
            this.Load += new System.EventHandler(this.ConnectToNowForm_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		private void btnConnect_Click(object sender, System.EventArgs e)
		{
            if (this.txtEmail.Text.IndexOfAny("@!`~}{()*&^%$#'\",?\\[]".ToCharArray()) == -1)
            {
                string str = (string)txtApplications.SelectedItem;
                HttpApplication app = Start.XedusState.Applications[str];
                int port = 4274;

                //AsyncCallback acb = new AsyncCallback(delegate(IAsyncResult res) {

                //    string userid = finder.EndRegisterUser(res);

                //    if (userid != null)
                //    {
                //        Start.Email = this.txtEmail.Text;
                //        Start.XedusState.XedusSettings.LastEmail = Start.Email;
                //        MessageBox.Show("Connected");
                //        //Start.Connected = true;
                //    }
                //    else
                //    {
                //        MessageBox.Show("Either your username or password is incorrect");
                //    }	
                
                //});
                
                
            }
            else
            {
                MessageBox.Show("name cannot contain any special characters");
            }
		}
		void FinishRegister(IAsyncResult res)
		{
            
	
            //this.Close();
		}

        private void ConnectToNowForm_Load(object sender, EventArgs e)
        {

        }

	}
}
